package com.ww.editsms;

import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import com.chinawangw.R;
import com.ww.editsms.adapter.SMSAdapter;
import com.ww.editsms.db.Cursordb;
import com.ww.editsms.entity.Person;
import com.ww.editsms.entity.SMS;
import com.ww.editsms.util.SmsUtil;
import com.ww.editsms.widget.SearchEditText;

public class SmsListActivity extends MyActivity implements OnClickListener {
	ListView listView;
	SearchEditText search_Keywords;
	Button button_search;
	public static List<Person> list;
	private SMSAdapter adpter;
	List<SMS> smsList;
	Person person;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms_list);
		setTitle("短信");
		setRightButton(R.drawable.biz_news_column_drag_pressed);

		listView = (ListView) findViewById(R.id.listView);
		search_Keywords = (SearchEditText) findViewById(R.id.search_Keywords);
		button_search = (Button) findViewById(R.id.button_search);
		right = (ImageButton) findViewById(R.id.right);
		right.setOnClickListener(this);
		button_search.setOnClickListener(butSearchClick);
		getPhoneContacts();
	}

	OnClickListener butSearchClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (SmsUtil.isNotEmpty(search_Keywords.getText().toString())) {
				List<SMS> list = SmsUtil.filter(smsList, search_Keywords.getText().toString());
				adpter = new SMSAdapter(SmsListActivity.this,person.getNumber(), list, true);
				listView.setAdapter(adpter);
			} else {
				adpter = new SMSAdapter(SmsListActivity.this,person.getNumber(), smsList, false);
				listView.setAdapter(adpter);
			}
		}
	};


	public void getPhoneContacts() {
		showDialog(0);
		new Thread() {
			public void run() {
				person = new Person();
				person.setName(getIntent().getStringExtra("name"));
				person.setNumber(getIntent().getStringExtra("phone"));

				smsList = Cursordb.getSmsInPhone(SmsListActivity.this, person.getNumber());
				person.setSmsList(smsList);
				handler.sendEmptyMessage(1);
			}
		}.start();
	};

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			adpter = new SMSAdapter(SmsListActivity.this,person.getNumber(), smsList, false);
			listView.setAdapter(adpter);
			listView.setSelection(smsList.size() - 1);
		};
	};

	@Override
	public void onClick(View v) {
		final ListRadioDialog a = new ListRadioDialog(this);
		a.hideButton();
		a.show(new String[] { "添加收件", "添加发件" }, new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				a.cancel();
				Intent intent = new Intent();
				Bundle mBundle = new Bundle();
				SMS sms = new SMS();
				sms.setPhoneNum(person.getNumber());
				sms.setBody("");
				sms.setTime(new Date().getTime() + "");
				switch (position) {
				case 0:
					insertInBox(intent, mBundle, sms);
					break;
				case 1:
					insertSend(intent, mBundle, sms);
					break;
				}

			}
		});
	}

	public void insertInBox(Intent intent, Bundle mBundle, SMS sms) {
		sms.setType(1);
		sms.setOperateType(1);
		mBundle.putParcelable("sms", sms);
		intent.putExtras(mBundle);
		intent.setClass(SmsListActivity.this, SmsActivity.class);
		startActivityForResult(intent, 0);
	}

	public void insertSend(Intent intent, Bundle mBundle, SMS sms) {
		sms.setType(2);
		sms.setOperateType(2);
		mBundle.putParcelable("sms", sms);
		intent.putExtras(mBundle);
		intent.setClass(SmsListActivity.this, SmsActivity.class);
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == 0) {
			smsList = Cursordb.getSmsInPhone(SmsListActivity.this, person.getNumber());
			adpter.setList(smsList);
			adpter.notifyDataSetChanged();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
